<?php defined('SYSPATH') or die('No direct script access.');

// Категории
class Model_Categories
{
    public function get_top_categories()
    {
        $categories = $this->get_categories(3);
        return $categories;
    }
    
    public function get_categories($parent_id)
    {
        $query = DB::select()
                    ->from('categories')
                    ->where('parent_id', '=', $parent_id);
        
        $categories = $query->execute()->as_array();
        
        return $categories;
    }
    
    public function get_breadcrumbs($category_id)
    {
        $breadcrumbs = array();
        $old_category_id = $category_id;
        
        while($category_id != 3)
        {
            $query = DB::select()
                    ->from('categories')
                    ->where('category_id', '=', $category_id);
        
            $categories  = $query->execute()->as_array();
                        
            if(($category_id != $old_category_id) && count($categories)) {
                $breadcrumbs[] = $categories[0];   
            }
            
            if(count($categories)) {
                $category_id = $categories[0]['parent_id'];
            } else {
                $category_id = 3; 
            } 
        }
        
        $breadcrumbs = array_reverse($breadcrumbs);
        return $breadcrumbs;
    }
    
    public function get_child_categories($category_id)
    {
        $query = DB::select('children')
                ->from('categories')
                ->where('category_id', '=', $category_id);
                
        $category = $query->execute()->as_array();
        
        $ids = explode(',', $category[0]['children']);
        
        return $ids; 
    }
    
    public function get_category($category_id)
    {
        $query = DB::select()
                ->from('categories')
                ->where('category_id', '=', $category_id);
        
        $categories = $query->execute()->as_array();
        
        return $categories[0];  
    }
} // End Categories